import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

/// SmartDialog帮助类，封装所有对话框展示方法
class SmartDialogHelper {
  /// 显示普通对话框
  static Future<T?> show<T>({
    required Widget Function(BuildContext) builder,
    bool barrierDismissible = true,
    Color? barrierColor,
    Alignment alignment = Alignment.bottomCenter,
    Duration duration = const Duration(milliseconds: 260),
    String? tag,
    Duration? displayTime,
    bool usePenetrate = false,
  }) {
    return SmartDialog.show<T>(
      builder: builder,
      maskColor: barrierColor ?? Colors.black.withOpacity(0.3),
      maskWidget: null,
      usePenetrate: usePenetrate,
      keepSingle: false,
      animationType: SmartAnimationType.scale,
      onDismiss: null,
      displayTime: displayTime,
      onMask: barrierDismissible ? () => SmartDialog.dismiss() : null,
      tag: tag,
      alignment: alignment,
    );
  }

  /// 显示加载对话框
  static Future<void> showLoading({
    String msg = "",
    bool clickMaskDismiss = false,
    Widget Function(BuildContext)? builder,
    Alignment alignment = Alignment.bottomCenter,
    Duration? displayTime,
    Duration duration = const Duration(milliseconds: 260),
    Color barrierColor = Colors.black54,
    Color? backgroundColor,
    Color? loadingColor,
    bool usePenetrate = false,
  }) {
    return SmartDialog.showLoading(
      builder:
          builder ??
          (context) => _build_loading_widget(msg, backgroundColor, loadingColor),
      maskColor: barrierColor,
      displayTime: displayTime,
      usePenetrate: usePenetrate,
      onMask: clickMaskDismiss ? () => SmartDialog.dismiss() : null,
      alignment: alignment,
    );
  }

  /// 构建加载Widget
  static Widget _build_loading_widget(
    String msg,
    Color? backgroundColor,
    Color? loadingColor,
  ) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 20),
      decoration: BoxDecoration(
        color: backgroundColor ?? Colors.black87,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation(loadingColor ?? Colors.white),
          ),
          if (msg.isNotEmpty) ...[
            const SizedBox(height: 10),
            Text(
              msg,
              textAlign: TextAlign.center,
              style: const TextStyle(
                color: Colors.white,
                fontSize: 15,
                fontWeight: FontWeight.w500,
              ),
            ),
          ],
        ],
      ),
    );
  }

  /// 显示Toast
  static Future<void> show_toast(
    String msg, {
    Widget Function(BuildContext)? builder,
    Alignment alignment = Alignment.bottomCenter,
    Duration? displayTime,
    Color? backgroundColor,
    Color? textColor,
    double? borderRadius,
    EdgeInsets? padding,
  }) {
    return SmartDialog.showToast(
      msg,
      builder:
          builder ??
          (context) => Container(
            padding:
                padding ??
                const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
            decoration: BoxDecoration(
              color: backgroundColor ?? Colors.black87,
              borderRadius: BorderRadius.circular(borderRadius ?? 20),
            ),
            child: Text(
              msg,
              textAlign: TextAlign.center,
              style: TextStyle(
                color: textColor ?? Colors.white,
                fontSize: 15,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
      displayTime: displayTime ?? const Duration(seconds: 2),
      alignment: alignment,
    );
  }

  /// 关闭对话框
  static Future<void> dismiss({
    Duration duration = const Duration(milliseconds: 300),
    bool force = false,
    String? tag,
  }) {
    return SmartDialog.dismiss(force: force, tag: tag);
  }

  /// 显示Snackbar样式通知
  static Future<void> show_snackbar({
    required String title,
    required String message,
    Duration? displayTime,
    Color? backgroundColor,
    Color? colorText,
    Widget? icon,
    Function? onTap,
    bool showCloseIcon = true,
    Alignment alignment = Alignment.bottomCenter,
  }) {
    return SmartDialog.showNotify(
      msg: message,
      notifyType: NotifyType.success,
      displayTime: displayTime ?? const Duration(seconds: 3),
      alignment: alignment,
      builder: (context) {
        return Container(
          margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: backgroundColor ?? const Color(0xFF222222),
            borderRadius: BorderRadius.circular(20),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.1),
                blurRadius: 10,
                offset: const Offset(0, 4),
              ),
            ],
          ),
          child: InkWell(
            onTap: () {
              SmartDialog.dismiss();
              if (onTap != null) onTap();
            },
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  children: [
                    if (icon != null) ...[icon, const SizedBox(width: 8)],
                    Expanded(
                      child: Text(
                        title,
                        style: TextStyle(
                          color: colorText ?? Colors.white,
                          fontWeight: FontWeight.bold,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    if (showCloseIcon)
                      InkWell(
                        onTap: () => SmartDialog.dismiss(),
                        child: Icon(
                          Icons.close,
                          color: colorText ?? Colors.white,
                          size: 18,
                        ),
                      ),
                  ],
                ),
                if (message.isNotEmpty) ...[
                  const SizedBox(height: 6),
                  Text(
                    message,
                    style: TextStyle(
                      color: colorText ?? Colors.white,
                      fontSize: 14,
                    ),
                  ),
                ],
              ],
            ),
          ),
        );
      },
    );
  }

  /// 显示错误提示
  static Future<void> show_error(String message, {Duration? display_time}) {
    return show_snackbar(
      title: "error",
      message: message,
      backgroundColor: Colors.red.shade700,
      icon: const Icon(Icons.error_outline, color: Colors.white, size: 20),
      displayTime: display_time ?? const Duration(seconds: 3),
    );
  }

  /// 显示警告提示
  static Future<void> show_warning(String message, {Duration? display_time}) {
    return show_snackbar(
      title: "warning",
      message: message,
      backgroundColor: Colors.orange.shade700,
      icon: const Icon(
        Icons.warning_amber_outlined,
        color: Colors.white,
        size: 20,
      ),
      displayTime: display_time ?? const Duration(seconds: 3),
    );
  }

  /// 显示成功提示 - 绿色植物风格
  static Future<void> show_success(String message, {Duration? display_time}) {
    return SmartDialog.showNotify(
      msg: message,
      notifyType: NotifyType.success,
      displayTime: display_time ?? const Duration(seconds: 2),
      alignment: Alignment.bottomCenter,
      builder: (context) {
        return Container(
          margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
          padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 16.h),
          decoration: BoxDecoration(
            color: const Color(0xFFCDFF5F),
            borderRadius: BorderRadius.circular(16.r),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.1),
                blurRadius: 8.r,
                offset: Offset(0, 3.h),
              ),
            ],
          ),
          child: Row(
            children: [
              Icon(
                Icons.check_circle_outline,
                color: Color(0xFF222222),
                size: 24.sp,
              ),
              SizedBox(width: 12.w),
              Expanded(
                child: Text(
                  message,
                  style: TextStyle(
                    color: Color(0xFF222222),
                    fontWeight: FontWeight.w600,
                    fontSize: 15.sp,
                  ),
                ),
              ),
              InkWell(
                onTap: () => SmartDialog.dismiss(),
                child: Icon(Icons.close, color: Color(0xFF222222), size: 20.sp),
              ),
            ],
          ),
        );
      },
    );
  }

  /// 显示信息提示 - 弹出卡片风格
  static Future<void> show_info(String message, {Duration? display_time}) {
    return SmartDialog.showNotify(
      msg: message,
      notifyType: NotifyType.success,
      displayTime: display_time ?? const Duration(seconds: 3),
      alignment: Alignment.bottomCenter,
      builder: (context) {
        return Container(
          margin: EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.h),
          padding: EdgeInsets.all(16.w),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20),
            boxShadow: [
              BoxShadow(
                color: Color(0x26000000), // 透明度15%的黑色
                blurRadius: 15,
                offset: Offset(0, 5),
              ),
            ],
          ),
          child: Row(
            children: [
              Container(
                padding: EdgeInsets.all(8.w),
                decoration: BoxDecoration(
                  color: const Color(0x33CDFF5F), // 透明度20%的主题绿色
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Icon(
                  Icons.lightbulb_outline,
                  color: Color(0xFF222222),
                  size: 24.sp,
                ),
              ),
              SizedBox(width: 16.w),
              Expanded(
                child: Text(
                  message,
                  style: TextStyle(
                    color: Color(0xFF222222),
                    fontSize: 15.sp,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
              InkWell(
                onTap: () => SmartDialog.dismiss(),
                child: Icon(Icons.close, color: Color(0xFF222222), size: 20.sp),
              ),
            ],
          ),
        );
      },
    );
  }

  /// 显示通知 - 简约顶部条形风格
  static Future<void> show_top_bar(
    String message, {
    Color? backgroundColor,
    Color? textColor,
    IconData? icon,
    Duration? display_time,
  }) {
    return SmartDialog.showNotify(
      msg: message,
      notifyType: NotifyType.success,
      displayTime: display_time ?? const Duration(seconds: 2),
      alignment: Alignment.topCenter,
      builder: (context) {
        return Container(
          width: double.infinity,
          padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 20.w),
          decoration: BoxDecoration(
            color: backgroundColor ?? const Color(0xFF222222),
            boxShadow: [
              BoxShadow(
                color: Color(0x1A000000), // 透明度10%的黑色
                blurRadius: 4,
                offset: Offset(0, 2),
              ),
            ],
          ),
          child: SafeArea(
            child: Row(
              children: [
                if (icon != null) ...[
                  Icon(icon, color: textColor ?? Colors.white, size: 20),
                  SizedBox(width: 10.w),
                ],
                Expanded(
                  child: Text(
                    message,
                    style: TextStyle(
                      color: textColor ?? Colors.white,
                      fontSize: 15.sp,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  /// 显示动态提示 - 带图标切换效果
  static Future<void> show_animated_toast(
    String message, {
    bool isSuccess = true,
    Duration? display_time,
  }) {
    final Color bgColor = isSuccess ? const Color(0xFFCDFF5F) : Colors.white;

    final Color textColor = const Color(0xFF222222);

    final IconData iconData = isSuccess
        ? Icons.check_circle
        : Icons.info_outline;

    return SmartDialog.showToast(
      message,
      displayTime: display_time ?? const Duration(seconds: 2),
      alignment: Alignment.bottomCenter,
      builder: (context) {
        return Container(
          padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 16.h),
          decoration: BoxDecoration(
            color: bgColor,
            borderRadius: BorderRadius.circular(50),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.15),
                blurRadius: 10,
                offset: const Offset(0, 3),
              ),
            ],
          ),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Icon(iconData, color: textColor, size: 24.sp),
              SizedBox(width: 12.w),
              Text(
                message,
                style: TextStyle(
                  color: textColor,
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
